home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / ddx / sprite.X11R3 / spriteIo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-08  |  5.6 KB  |  214 lines

  1. /*-
  2.  * spriteIo.c --
  3.  *    Functions to handle input from the keyboard and mouse.
  4.  *
  5.  * Copyright (c) 1987, 1989 by the Regents of the University of California
  6.  *
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  *
  16.  */
  17. #ifndef lint
  18. static char rcsid[] =
  19.     "$Header: spriteIo.c,v 1.3 88/03/27 21:17:12 deboor Exp $ SPRITE (Berkeley)";
  20. #endif lint
  21.  
  22. #include    "spriteddx.h"
  23. #include    <sys/time.h>
  24.  
  25.  
  26. Bool              screenSaved = FALSE;
  27. /*
  28.  * lastEventTime is used by the OS layer to decide when to do the screen
  29.  * saver stuff. lastEventTimeMS is used by the cursor module when it generates
  30.  * motion events in spriteSetCursorPosition. This is necessary because on
  31.  * sun2's, the time in the keyboard events is relative to boot time, while
  32.  * on sun3's it's real time, so what can you do?
  33.  */
  34. unsigned int         lastEventTime = 0;
  35. unsigned int      lastEventTimeMS = 0;
  36.  
  37. int              spriteCheckInput = 0;
  38. #define RESTORE_CURSOR    0x01
  39. #define READ_INPUT    0x02
  40.  
  41. /*-
  42.  *-----------------------------------------------------------------------
  43.  * spriteCursorGone --
  44.  *    This is a function required by the cursor code which is
  45.  *    just like spriteInputAvail, except it marks that the cursor
  46.  *    needs to be restored.
  47.  *
  48.  * Results:
  49.  *    None.
  50.  *
  51.  * Side Effects:
  52.  *    spriteCheckInput is altered to contain the RESTORE_CURSOR flag.
  53.  *
  54.  *-----------------------------------------------------------------------
  55.  */
  56. void
  57. spriteCursorGone()
  58. {
  59.     spriteCheckInput |= RESTORE_CURSOR;
  60. }
  61.  
  62. /*-
  63.  *-----------------------------------------------------------------------
  64.  * spriteInputAvail --
  65.  *    This function is called from the scheduler whenever one of
  66.  *    the devices we told it to look for has input waiting.
  67.  *
  68.  * Results:
  69.  *    None.
  70.  *
  71.  * Side Effects:
  72.  *    spriteCheckInput has its READ_INPUT flag set.
  73.  *
  74.  *-----------------------------------------------------------------------
  75.  */
  76. void
  77. spriteInputAvail()
  78. {
  79.     spriteCheckInput |= READ_INPUT;
  80. }
  81.  
  82. /*-
  83.  *-----------------------------------------------------------------------
  84.  * SetTimeSinceLastInputEvent --
  85.  *    Set the lastEventTime to now.
  86.  *
  87.  * Results:
  88.  *    None.
  89.  *
  90.  * Side Effects:
  91.  *    lastEventTime is altered.
  92.  *
  93.  *-----------------------------------------------------------------------
  94.  */
  95. void
  96. SetTimeSinceLastInputEvent()
  97. {
  98.     struct timeval now;
  99.  
  100.     gettimeofday(&now, (struct timezone *) NULL);
  101.     lastEventTime = (now.tv_usec/1000) + (now.tv_sec*1000);
  102. }
  103.  
  104. /*-
  105.  *-----------------------------------------------------------------------
  106.  * TimeSinceLastInputEvent --
  107.  *    Function used for screensaver purposes by the os module.
  108.  *
  109.  * Results:
  110.  *    The time in milliseconds since there last was any
  111.  *    input.
  112.  *
  113.  * Side Effects:
  114.  *    None.
  115.  *
  116.  *-----------------------------------------------------------------------
  117.  */
  118. int
  119. TimeSinceLastInputEvent()
  120. {
  121.     struct timeval now;
  122.     int millis;
  123.  
  124.     gettimeofday(&now, (struct timezone *) NULL);
  125.     millis = (now.tv_usec/1000) + (now.tv_sec*1000);
  126.  
  127.     if (lastEventTime == 0) {
  128.     lastEventTime = millis;
  129.     }
  130.     return millis - lastEventTime;
  131. }
  132.  
  133. /*-
  134.  *-----------------------------------------------------------------------
  135.  * ProcessInputEvents --
  136.  *    Retrieve all waiting input events and pass them to DIX in their
  137.  *    correct chronological order. The keyboard driver is responsible
  138.  *    for reading all the events (both for the mouse and the keyboard).
  139.  *
  140.  * Results:
  141.  *    None.
  142.  *
  143.  * Side Effects:
  144.  *    Events are passed to the DIX layer.
  145.  *
  146.  *-----------------------------------------------------------------------
  147.  */
  148. void
  149. ProcessInputEvents ()
  150. {
  151.     register Mouse_Event    *events;         /* Array of events */
  152.     register int          numEvents;        /* Number of events left */
  153.     int                      nE;                /* Total number of events */
  154.     DevicePtr            pPointer;        /* System pointer */
  155.     DevicePtr            pKeyboard;        /* System keyboard */
  156.     register PtrPrivPtr     ptrPriv;        /* Private data for pointer */
  157.     register KbPrivPtr        kbdPriv;        /* Private data for keyboard */
  158.     Mouse_Event          *lastEvent;        /* Last event processed */
  159.     enum {
  160.     NoneYet, Ptr, Kbd
  161.     }                lastType = NoneYet;    /* Type of last event */
  162.  
  163.     if (spriteCheckInput & READ_INPUT) {
  164.     pPointer = LookupPointerDevice();
  165.     pKeyboard = LookupKeyboardDevice();
  166.     
  167.     ptrPriv = (PtrPrivPtr)pPointer->devicePrivate;
  168.     kbdPriv = (KbPrivPtr)pKeyboard->devicePrivate;
  169.     
  170.     /*
  171.      * Get events from both the pointer and the keyboard via the keyboard's
  172.      * GetEvents vector. The number of events read is stored in numEvents.
  173.      */
  174.     events = (* kbdPriv->GetEvents) (pKeyboard, &nE);
  175.     numEvents = nE;
  176.     
  177.     while (numEvents) {
  178.         if (events->flags & KEYBOARD_EVENT) {
  179.         if (lastType == Ptr) {
  180.             (* ptrPriv->DoneEvents) (pPointer, FALSE);
  181.             lastType = Kbd;
  182.         }
  183.         (* kbdPriv->ProcessEvent) (pKeyboard, events);
  184.         } else if (events->flags & MOUSE_EVENT) {
  185.         if (lastType == Kbd) {
  186.             (* kbdPriv->DoneEvents) (pKeyboard, FALSE);
  187.             lastType = Ptr;
  188.         }
  189.         (* ptrPriv->ProcessEvent) (pPointer, events);
  190.         } else {
  191.         /* ??? */
  192.         }
  193.         lastEvent = events;
  194.         lastEventTimeMS = events->time;
  195.         numEvents -= 1;
  196.         events += 1;
  197.     }
  198.     
  199.     SetTimeSinceLastInputEvent();
  200.     if (screenSaved) {
  201.         SaveScreens(SCREEN_SAVER_FORCER, ScreenSaverReset);
  202.     }
  203.     
  204.     (* kbdPriv->DoneEvents) (pKeyboard, TRUE);
  205.     (* ptrPriv->DoneEvents) (pPointer, TRUE);
  206.     }
  207.  
  208.  
  209.     if (spriteCheckInput & RESTORE_CURSOR) {
  210.     spriteRestoreCursor();
  211.     }
  212.     spriteCheckInput = 0;
  213. }
  214.